package com.example.demo.common;

import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 验证码过期定时
 */
@Component
public class expire {
    //生成随机数
  public static Random random = new Random();
  //保存当前随机数
  private static Integer currentNumber;

  public static int sendEmail(){
      //设置当前随机数已经过期，失效
      if (currentNumber != null){
          currentNumber = null;
      }
      //生成新的随机数发送
      int SendCaptcha = random.nextInt(10_00000-100000)+100000;

      //定时任务
      Timer timer = new Timer();
      timer.schedule(new TimerTask() {
          @Override
          public void run() {
              //设置当前随机数失效 过期前
              currentNumber = null;
          }
      },3000);
      //将当前随机数设置为新的随机数
      currentNumber = SendCaptcha ;
      //随机数没有失效，返回当前随机数
      if (currentNumber != null){
          return SendCaptcha;
      }else {
          return -1;
      }
  }
}
